#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void split_free(char **str,int len)
{
    int i;
    for(i = 0;i<len;i++)
    {
		if(str[i] != NULL)
		{
			free(str[i]);
			str[i] = NULL;
		}
    }
	if(str != NULL)
    {
    	free(str);
    	str = NULL;
    }

}

char **split(const char *source,const char spl,int *count)
{
    char **result;
    size_t len = strlen(source);
    if(len == 0)
    {
        printf("the argument length is 0,please check the param\n");
        return NULL;
    }
    char scp[len + 1];
    memset(scp,0x00,sizeof(scp));
    sprintf(scp,"%s",source);
    int num = 0 , total = 0 , str_num = 0;
    while(num < len)
    {
        if(scp[num] == spl)
        {
            total++;
        }
        num++;
    }
    result = (char **)malloc(sizeof(char *) * (total + 1));
    if(!result)
    {
        printf("%d is failed..",__LINE__);
        return NULL;
    }
    num = 0;
    while(str_num <= total)
    {
        int j = 0 , count = 1;
        result[str_num] = (char *)malloc(sizeof(char) * 10);
        if(!result[str_num])
        {
            printf("%d is failed..",__LINE__);
            split_free(result,str_num);
            return NULL;
        }
		memset(result[str_num],0x00,sizeof(result[str_num]));
        while(num < len)
        {
            if(num == count * 10)
            {
                count++;
                result[str_num] = (char *)realloc(result[str_num],sizeof(char) * count * 10);
                if(!result[str_num])
                {
                    printf("%d is failed..",__LINE__);
                    split_free(result,str_num);
                    return NULL;
                }
            }
            if(scp[num] == spl)
            {
                num++;
                break;
            }
            result[str_num][j] = scp[num];
            j++;
            num++;
        }
        str_num++;
    }
    *count = str_num;
    return result;
}

